<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item border-none" required label="标题"><u-input v-model="form.title" disabled type="text" placeholder="请输入" /></u-form-item>

				<u-form-item class="form-item border-none" required label="活动时间">
					<!-- <u-input v-model="form.contactTime" type="text" placeholder="请输入" /> -->
					<date-time-picker placeholder="请选择" fields="day" @change="timeChange($event, 'contactTime')"></date-time-picker>
				</u-form-item>

				<u-form-item class="form-item border-none" required label="走访群众"><u-input v-model="form.contactPeople" type="text" placeholder="请输入" /></u-form-item>

				<u-form-item class="form-item border-none" required label="" labelPosition="top" prop="contactContent">
					<u-input inputAlign="left" v-model="form.contactContent" type="textarea" placeholder="请输入活动内容" :clearable="false" :height="160" :maxlength="-1" />
				</u-form-item>

				<u-form-item class="form-item" labelPosition="top">
					<!-- block -->
					<uni-file-upload :type="['all', 'image', 'audio']" :limit="10"></uni-file-upload>
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm">
			<u-button class="form-btn" type="error" @click="confirm">提交</u-button>
			<!-- <view class="flex-box">
				<u-button class="form-btn" type="error" @click="confirm">提交</u-button>
				<u-button class="form-btn" type="success" @click="spare">草稿</u-button>
			</view> -->
		</view>

		<view v-if="false && willClue"></view>
	</view>
</template>

<script>
import uniFileUpload from '@/components/uni-file-uploader.vue';
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		uniFileUpload,
		dateTimePicker
	},
	data() {
		return {
			id: '',
			title: '上传活动情况',
			typeA: '',

			form: {
				id: '',
				title: '代表走访联系群众情况',
				dataType: '4',
				createBy: '',
				contactTime: '',
				contactPeople: '',
				contactContent: ''
			},
			rules: {
				title: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				contactContent: [
					{
						required: true,
						message: '请输入活动内容',
						trigger: 'change'
					}
				]
			},
			willClue: {
				adoptUser: '',
				clientCode: ''
			}
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.id) {
			this.id = option.id;
			if (option.type == 'spare') {
				this.title = '修改活动情况';
				this.typeA = 'spare';
				this.spareData();
			}
		}

		uni.setNavigationBarTitle({
			title: this.title
		});
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	onUnload() {
		this.$store.commit('filePush', []);
	},
	computed: {
		...mapState(['userInfo', 'fileList'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
			} else {
				this.$logout();
			}
		},
		clueData() {},
		spareData() {
			this.$request('/contactGroup/msContactGroup/get', {
				id: this.id,
				userId: this.userId
			}).then((res) => {
				// Object.assign(this.form, res.data);
				Object.keys(this.form).forEach((key) => (this.form[key] = res.data[key]));
				this.$store.commit('filePush', res.data.fileList);

				let regx = /<[^>]*>|<\/[^>]*>/gm;
				if (res.data.contactContent) {
					this.form.contactContent = res.data.contactContent.replace(regx, '');
				}
			});
		},
		spare() {
			this.form.nodeStatus = '0';
			this.confirm();
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute2 + ':' + '00';
		},
		navTo(e) {
			uni.navigateTo({
				url: e
			});
		},
		back(e) {
			uni.navigateBack({});
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					console.log('验证失败');
					return false;
				} else {
					let data = {};
					this.form.clientCode = this.clientCode;
					this.form.createBy = this.userId;

					data = {
						...this.form,
						fileList: this.fileList
					};

					let url = '/contactGroup/msContactGroup/save';
					this.$request(url, data, {
						load: true
					}).then((res) => {
						this.$msg(res.msg);
						setTimeout(() => {
							let pre = this.$prePage();
							if (pre && pre.loadData) {
								pre.loadData('refresh');
							}
							uni.navigateBack();
						}, 500);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.popup-box {
	position: fixed;
	left: 0;
	top: 0;
	right: 0;
	bottom: 0;
	z-index: 1000;
	padding: 30rpx;
	background-color: #fff;

	.h2 {
		margin-bottom: 26rpx;
		font-size: 18px;
		font-weight: 600;
		color: #333;
		line-height: 48rpx;
		text-align: center;
	}

	.summon {
		white-space: pre-wrap;
		font-size: 17px;
		line-height: 54rpx;

		.p {
			margin-bottom: 1em;
		}
	}
}

.popup-flex {
	display: flex;
	flex-direction: column;
	height: 100%;
}

.m-box {
	flex-wrap: nowrap;
	flex: 1;
	height: 0;
}

.node {
	margin: 20rpx 0 0 20rpx;
	font-size: 14px;
	color: #9b9b9b;
	line-height: 120rpx;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid #f5f7fa;

		&.border-none {
			border-bottom: 0;
		}
	}

	.form-right {
		display: flex;
		justify-content: flex-end;
		width: 100%;
		text-align: right;

		.image {
			margin-left: 20rpx;
			width: 70rpx;
			height: 70rpx;
		}
	}

	.form-checkbox {
		// padding: 8rpx 0;
		margin: -8rpx 0;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
